

            The protocol for the PowerModules
            
                    (up to V0.10)



The PowerModules can be connected together via an UTP or STP
patch cable. The physical layer uses RS485. So it is neccessary
to reach the PMs through different addresses.

With the current hardware revision it is neccessary to connect the
RS232 line to the PM with the address 0 (factory default)


All of the datagrams started with an address field.
To make it failsafe and easy to detect, this field has the MSB set to 1.
All other bytes which are transmitted must below the value 128 dezimal.
So it is easy to synchronize if a message was damaged.
After the address field a command field follows.

The following commands are available:

	READVERSION			10h		(> fpm0-08)

    READINFO            20h

    READSTATE           30h
    
    SWITCHON            31h
    SWITCHOFF           32h
    SWITCHALL           33h     (> fpm0-05)
    SWITCHALLDELAYED    34h     (> fpm0-05)
    SWITCHTOGGLE        35h     (> fpm0-05)
    SWITCHPULSEON       36h     (> fpm0-05)
    SWITCHPULSEOFF      37h     (> fpm0-05)

    GETNAME				41h		(> fpm0-08)
    SETNAME				42h		(> fpm0-08)


After the commandfield there maybe additional parameter bytes.
This depends on the used command, please look at the command description.

If a switch command is used, than the last byte is always a one byte xor
checksum. But without the MSB.


                    !!! Attention !!!

    In the whole protocol are only one byte with the MSB set.
    
                        The addressbyte.



Commanddescription:


READVERSION

After this command the device returns a string with
the software version. The first byte is the length of the string.


READINFO

After this command the device sends one byte back. The device ID.
For the PowerModule are two IDs valid:

    1   PowerModule 4
    2   PowerModule 8
    
    
READSTATE    

After this command the device sends one byte back. The current state.
This byte reflects the state of the outlets in a binary form.
So if 12 is the answer, it should be converted to the binary system.

                        12d -> 1010b
                        
So outlet 4 and 2 are on, outlet 3 and 1 are off.


SWITCHON

One or all outlets can be switched on with this command.


SWITCHOFF

One or all outlets can be switched off with this command.


SWITCHALL

Switch all allows to switch all outlets to a deticated state. Not only
'on' or 'off' for all outlets at the same time.


SWITCHALLDELAYED

Same like SWITCHALL, but the outlets are not switched simultaniously.
They switch with a short delay between, to avoid high inrush currents.


SWITCHTOGGLE

The state of one outlet can be toggled. If it is on, it is switched off.
If it is off, it is switched on.


SWITCHPULSEON

The choosen outlet is switched on for a time between 100ms and 1638.3s.
After this time the outlet is switched off.


SWITCHPULSEOFF

The choosen outlet is switched off for a time between 100ms and 1638.3s.
After this time the outlet is switched on.



GETNAME

Get the name of the outlet or the whole device.



SETNAME

Set the name of the outlet or the whole device.





The datagrams in details


 1. Read Version

	transmit:	|Address + 80h|10h|
    
    receive:	|LENGTH|ASCII|...|ASCII|


    Example:

                    putchar(Address|0x80);
                    putchar(0x10);
                    
                    i = readchar(timeoutvalue);
                    
		    		while(i != 0) {
						
						printf("%c", readchar(timeoutvalue));
                        i--;
                        
					}



 2. Read Information

    transmit:   |Address + 80h|20h|

    receive:                         |type|



    Address can be: 0...31

    Type can be:    1   PM4
                    2   PM8


    A timeout should be used to detect if a PM is connected.


    Example:

                    putchar(Address|0x80);
                    putchar(0x20);

                    Type = readchar(timeoutvalue)



 3. Read Status

    transmit:   |Address + 80h|30h|

    receive:                        |state|


    Address can be: 0...31

    State can be:   0...FFh each bit represents the corresponding
                    outlet.


    Example:

                    putchar(Address|0x80);
                    putchar(0x30);

                    State = readchar(timeoutvalue)
             
             
             
                    
 4. Switch On

    transmit:   |Address + 80h|31h|outlet|checksum|


    Address can be: 0...31

    Outlet can be:  1...4 PM4
                    1...8 PM8
                    9     both, to switch all on
                                (Attention: maybe dangerous)

    Checksum:       the bitwise 'exclusive or' off the first 3 bytes
                    with MSB set to 0.

    Example:

                    putchar(Address|0x80);
                    putchar(0x31);
                    putchar(0x01);
                    putchar(((Address|0x80)^0x31^0x01)&0x7F);



 5. Switch Off

    transmit:   |Address + 80h|32h|outlet|checksum|


    Address can be: 0...31    

    Outlet can be:  1...4 PM4
                    1...8 PM8
                    9     both, to switch all off

    Checksum:       the bitwise 'exclusive or' off the first 3 bytes
                    with MSB set to 0.

    Example:

                    putchar(Address|0x80);
                    putchar(0x32);
                    putchar(0x01);
                    putchar(((Address|0x80)^0x32^0x01)&0x7F);
 
 
 6. Switch All
 
    transmit:   |Address + 80h|33h|upper outlets|lower outlets|checksum|
    
    
    Address can be: 0...31
    
    Upper outlets:  0...15  (0...1111b)
    Lower outlets:  0...15  (0...1111b)
    
    Checksum:       the bitwise 'exclusive or' off the first 4 bytes
                    with MSB set to 0.

    Example:

                    putchar(Address|0x80);
                    putchar(0x33);
                    putchar(0x00);
                    putchar(0x05);
                    putchar(((Address|0x80)^0x33^0x00^0x05)&0x7F);
                    
                    
 7. Switch All Delayed
 
    transmit:   |Address + 80h|34h|upper outlets|lower outlets|checksum|
    
    
    Address can be: 0...31
    
    Upper outlets:  0...15  (0...1111b)
    Lower outlets:  0...15  (0...1111b)
    
    Checksum:       the bitwise 'exclusive or' off the first 4 bytes
                    with MSB set to 0.

    Example:

                    putchar(Address|0x80);
                    putchar(0x34);
                    putchar(0x00);
                    putchar(0x05);
                    putchar(((Address|0x80)^0x34^0x00^0x05)&0x7F);
                    
                    
 8. Switch Toggle
 
    transmit:   |Address + 80h|35h|outlet|checksum|
    
    
    Address can be: 0...31
    
    Outlet can be:  1...4 PM4
                    1...8 PM8
    
    Checksum:       the bitwise 'exclusive or' off the first 3 bytes
                    with MSB set to 0.

    Example:

                    putchar(Address|0x80);
                    putchar(0x35);
                    putchar(0x01);
                    putchar(((Address|0x80)^0x35^0x01)&0x7F);
                    
                    
 9. Switch Pulse On
 
    transmit:   |Address + 80h|36h|outlet|high time|low time|checksum|
    
    
    Address can be: 0...31
    
    Outlet can be:  1...4 PM4
                    1...8 PM8

    Time:           14bit value truncated in 2 bytes.
    
    Checksum:       the bitwise 'exclusive or' off the first 5 bytes
                    with MSB set to 0.

    Example:
    				LowByte = (unsigned char) (Time & 0x007F);
                    HighByte = (unsigned char) (Time >> 7);

                    putchar(Address|0x80);
                    putchar(0x36);
                    putchar(0x01);
                    putchar(HighTimeByte);
                    putchar(LowTimeByte);
                    putchar(((Address|0x80)^0x36^0x01^HighByte^LowByte)&0x7F);


10. Switch Pulse Off
 
    transmit:   |Address + 80h|37h|outlet|high time|low time|checksum|
    
    
    Address can be: 0...31
    
    Outlet can be:  1...4 PM4
                    1...8 PM8

    Time:           14bit value truncated in 2 bytes.
    
    Checksum:       the bitwise 'exclusive or' off the first 5 bytes
                    with MSB set to 0.

    Example:

					LowByte = (unsigned char) (Time & 0x007F);
                    HighByte = (unsigned char) (Time >> 7);
                    
                    putchar(Address|0x80);
                    putchar(0x37);
                    putchar(0x01);
                    putchar(HighByte);
                    putchar(LowByte);
                    putchar(((Address|0x80)^0x37^0x01^HighByte^LowByte)&0x7F);
                    

11. Get Name

	transmit:	|Address + 80h|41h|outlet|checksum|

	receive:	|length|ASCII|...|ASCII|
	
	Address can be: 0...31
    
   	Outlet can be:  0...4 PM4
                    0...8 PM8

					0 represents the device itself 1..4/8 means the outlets
	
	Checksum:       the bitwise 'exclusive or' off the first 3 bytes
                    with MSB set to 0.


	Example:
    
               		putchar(Address|0x80);
               		putchar(0x41);
               		putchar(0x00);
               		putchar(((Address|0x80)^0x41^0x00)&0x7F);
			
            		length = readchar(timeoutvalue);
                    
		    		for(i = 0; i < length; i++ {
			
						Byte = readchar(timeoutvalue);
						Name[i] = Byte;
					};


12. Set Name

	transmit:	|Address + 80h|42h|outlet|length|ASCII|...|ASCII|checksum|

   	Address can be: 0...31
    
   	Outlet can be:  0...4 PM4
                    0...8 PM8

					0 represents the device itself 1..4/8 means the outlets
                  
    length:			the count of the following ASCII characters
	
	ASCII:			ASCII characters (32..127) representing the name.

	Checksum:       the bitwise 'exclusive or' off the bytes
                    with MSB set to 0.

	Example:
	
					putchar(Address|0x80);
           			putchar(0x42);
           			putchar(0x01);
                    putchar(5);
           			putchar('O');
           			putchar('u');
					putchar('t');
					putchar(' ');
					putchar('1');
           			putchar(((Address|0x80)^0x42^0x01^5^'O'^'u'^'t'^' '^'1')&0x7F);
                    